#!/bin/bash
env_check(){
  jq --version 2>/dev/null
  if [[ $? -ne 0 ]]
  then
    echo "install jq first , please concat zxh"
    exit
  fi
}
restart_app(){
  jarname=$(ls -lt /opt/apexapp/services/${server} | grep -E '^-' | awk '{print $NF}' | grep -E '.jar$' | head -n 1 )
  configname=$(ls -lt /opt/apexapp/services/${server} | grep -E '^-' | awk '{print $NF}' | grep -E '.yml$' | grep -E "(bootstrap|application)" | head -n 1 )
  jar -tf "/opt/apexapp/services/${server}/${jarname}" | grep -i "classfinal"
  if [[ $? -eq 1 ]]
  then
    cd /opt/apexapp/services/${server}
    nohup java -DAppname=${server} -Xmx2048M -Xms2048M -jar \
      "/opt/apexapp/services/${server}/${jarname}"   \
      --spring.config.local="/opt/apexapp/services/${server}/${configname}" >/dev/null 2>&1 &
    cd -
  else
    /opt/apexapp/.local/bin/ypstart -r 1 ${server}
  fi
}
echo "------$(date '+%Y-%m-%d %T')-------"
source ${HOME}/.bashrc
DIR=$(cd "$(dirname "$0")";pwd)
env_check
servers=""
host_name="127.0.0.1"
nacos_port=8848
gateway_port=8700
nacos_user_name="nacos"
nacos_password="nacos@haici5"
config_files=("/etc/zxhtom/config.json" "${HOME}/.config/zxhtom/config.json" "$DIR/config.json")
configuration=""
declare -A config
config["servers"]=${servers}
config["host_name"]=${host_name}
config["nacos_port"]=${nacos_port}
config["gateway_port"]=${gateway_port}
config["nacos_user_name"]=${nacos_user_name}
config["nacos_password"]=${nacos_password}
for config_file in ${config_files[@]}
do
  if [[ -f ${config_file} ]]
  then
    configuration=$(sudo cat ${config_file})
    keys=$(echo $configuration | jq -r 'keys[]')
    for key in ${keys[@]}
    do
      config[${key}]=$(echo $configuration | jq -r '.'"${key}"'')
    done
  fi
done
servers=${config["servers"]}
host_name=${config["host_name"]}
nacos_port=${config["nacos_port"]}
gateway_port=${config["gateway_port"]}
nacos_user_name=${config["nacos_user_name"]}
nacos_password=${config["nacos_password"]}
while getopts ":h:s:" opt
do
    case $opt in
        h)
        host_name=$OPTARG
        echo -e "\033[40;37m 您输入的主机$host_name\033[0m"
        ;;
        s)
        servers=$OPTARG
        echo -e "\033[40;37m 您输入的服务:$servers\033[0m"
        ;;
        ?)
        echo -e "\033[40;37m 未知参数,你可以这样使用 : sh reload.sh -ssld -h127.0.0.1 \033[0m"
        exit 1;;
    esac
done
echo "###################params start###################"
echo -e "\033[32mservers:$servers\033[0m"
echo -e "\033[32mhost_name:$host_name\033[0m"
echo -e "\033[32mnacos_port: $nacos_port\033[0m"
echo -e "\033[32mgateway_port:$gateway_port\033[0m"
echo -e "\033[32mnacos_user_name:$nacos_user_name\033[0m"
echo -e "\033[32mnacos_password:$nacos_password\033[0m"
echo "###################params end###################"
token=`curl -sX POST "http://${host_name}:${nacos_port}/nacos/v1/auth/users/login" -H "Content-Type: application/x-www-form-urlencoded" -d "username=${nacos_user_name}&password=${nacos_password}" | jq -r .accessToken`
for server in ${servers[@]}
do
  instance=`curl -sX GET  "http://${host_name}:${nacos_port}/nacos/v1/ns/instance/list?&accessToken=${token}&serviceName=${server}"`
  size=`echo $instance | jq -r '.hosts | length'`
  echo $server"---->"$size
  if [[ $size -lt 1 ]]
  then
    # ypstart -r 1 ${server} >/dev/null 2>&1
    restart_app
    if [[ $? -ne 0 ]] 
    then
      jar -tf "/opt/apexapp/services/${server}/"$(ls -lt /opt/apexapp/services/${server} | grep -E '^-' | awk '{print $NF}' | grep -E '.jar$' | head -n 1 ) | grep -i "classfinal"
      if [[ $? -eq 1 ]]
      then
        echo -e "\033[31m${server} . package is not encryption \033[0m" >&2
      fi
    fi
  fi
done

gateway_content=$(curl -s "http://${host_name}:${gateway_port}/sld/random/getReadomMd5" 2>/dev/null)
code=$(echo $gateway_content | jq .code)
if [[ "$code" == "200" ]]
then
  echo "gateway is successful"
else
  ypstart -r 1 nantianmen >/dev/null 2>&1
fi
